<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2019/12/18
 * Time: 15:25
 */
namespace App\Services\System;

use App\Common\Exception\ErrorException;
use Illuminate\Support\Facades\DB;
use App\Models\System\Group;

/**
 * 系统管理-组 (服务)
 */
class GroupService
{
    /**
     * @var \App\Repositories\System\GroupRepository $groupRepository
     */
    private $groupRepository;

    public function __construct()
    {
        $this->groupRepository = app('groupRepository');;
    }

    /**
     * 递归获取当前学校下所有数据
     * @author lxy
     * @datetime 2022-11-18
     */
    public function getGroupAllBySchool($request)
    {
       // dd($request);
        $school = auth()->user()->school();
        $group = Group::select('id','group_name as title')
                    ->where('id',$school->id)
                    ->whereNotIn('id',[12659,12817,13016,13501,16051,15200,15253,16569,15201,15202])
                    ->with(['children'=>function($q) use($school){
                        $q->where('parent_id','!=',0)->where('thread_id','=',$school->id)->whereNotIn('id',[12659,12817,13016,13501,16051,15200,15253,16569,15201,15202]);
                    }])
                    ->get()->toArray();
        
        $i = 0;
        if(isset($request['id']) && !empty($request['id'])){
            $ids = explode(',',$request['id']);
            foreach($group as $k=>&$v){
                if(!empty($v['children'])){
                    foreach($v['children'] as $k2=>&$v2){                
                        if(empty($v2['children'])){
                            if(in_array($v2['id'],$ids)){
                                 $v2['checked'] = true;
                                 $i++;
                            }
                        }else{
                            foreach($v2['children'] as $k3=>&$v3){
                                if(empty($v3['children'])){
                                    if(in_array($v3['id'],$ids)){
                                        $v3['checked'] = true;
                                        $i++;
                                    }
                                }                                                  
                            }
                        }
                    }
                }
            }
        }

        if($group){
            $group[0]['spread'] = true;
            $group[0]['checked_sum'] = $i;
        }

        return $group;
    }


    //*******************外部调用方法****************************************************
    public function getCore($id, $keyName = 'id',$columns = ['*']){
        return $this->groupRepository->get($id,$keyName,$columns);
    }

    // 获取所有学校
    public function getAllSchoolCore($columns = ['*']){
        $filters = [];
        $filters[] = ['group_flag', '=', 1];

        $sort = ['id' => 'asc'];

        return $this->groupRepository->getAll($filters,$sort,$columns);
    }

    //获取所有年级
    public function getAllGradeCore($school_id, $columns = ['*']){
        $filters = [];
        $filters[] = ['parent_id', '=', $school_id];
        $filters[] = ['group_flag', '=', 2];

        $sort = ['group_name' => 'asc'];

        return $this->groupRepository->getAll($filters,$sort,$columns);
    }

    //获取所有班级
    public function getAllClassCore($grade_id, $columns = ['*']){
        $filters = [];
        $filters[] = ['parent_id', '=', $grade_id];
        $filters[] = ['group_flag', '=', 3];

        $sort = ['id' => 'asc'];

        return $this->groupRepository->getAll($filters,$sort,$columns);
    }

    //获取所有班级
    public function getAllClassWithStudentsCore($requestData,$columns = ['*']){
        $filters = [];
        $filters[] = ['group_flag', '=', 3];
        $filters[] = ['parent_id', '=', $requestData['parent_id']];

        $sort = ['id' => 'asc'];
        $result = $this->groupRepository->getAll($filters,$sort,$columns);
        $classes = $result->toArray();
        $classArr = array_column($classes,'id');
        $students = DB::table('vw_user_group')->select(['id','user_name','group_id'])->whereIn('group_id',$classArr)->orderByDesc('group_id')->get();
        $returnData = [];
        foreach ($classes as $k => $class) {
            $returnData[$k] = $class;
            foreach ($students as $student) {
                if ($class['id'] == $student->group_id){
                    $returnData[$k]['students'][] = [
                        'user_id' => $student->id,
                        'user_name' => $student->user_name,
                        'group_id' => $student->group_id,
                    ];
                }
            }
        }
        return $returnData;
    }

    /**
     * @param array $idArr
     * @return array
     * @throws ErrorException
     */
    public function getGroupNameById($idArr)
    {
        if (!is_array($idArr)){
            throw new ErrorException('参数不为数组');
        }
        return $this->groupRepository->getGroupNameById($idArr);
    }


}
